var app = getApp();
//引入请求封装
import {
    request
} from "../../request/request.js"; //ES6 promise
// pages/myInfor/myInfor.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 静态图片存储路径
        img: '../../image/',
        // 用户板块展示
        infor: false,
        // 任务关联数
        push: '',
        join: '',
        // 用户信息
        user: ''
    },
    Data: {
        code: "",
        user: ""
    },
    Data2: {
        token: ''
    },
    // 获取用户信息
    getInfor() {
        let that = this
        // wx.getUserInfo({
        //   success(res){
        //     console.log(res)
        //   },fail(err){
        //     console.log(err)
        //   }
        // })
        wx.getUserProfile({
            desc: "将用于小程序页面展示",
            success(res) {
                let userInfor = {
                    userImg: res.userInfo.avatarUrl,
                    userName: res.userInfo.nickName,
                    userSex: res.userInfo.gender
                }
                that.Data.user = userInfor
                wx.login({
                    timeout: 2000,
                    success(res) {
                        that.Data.code = res.code
                        that.login()
                    }
                })
                wx.setStorage({
                    key: "userInfor",
                    data: userInfor,
                    success(res) {
                        that.setData({
                            userImg: userInfor.userImg,
                            name: userInfor.name,
                            infor: true
                        })
                    }
                })
            },
            fail(err) {
                console.log(err)
            }
        })
    },
    // 登录请求
    async login() {
        let that = this
        const res = await request({
            url: "/login",
            data: this.Data,
            method: "post"
        })
        console.log(res)
        if (res.status == 200) {
            wx.setStorage({
                key: 'token',
                data: res.token,
                success() {
                    that.Data2.token = res.token
                    that.getUserInfor()
                }
            })
        }
    },
    // 请求用户信息
    async getUserInfor() {
        let that = this
        const res = await request({
            url: "/getUserInfor",
            data: this.Data2,
            method: "get"
        })
        console.log(res)
        that.setData({
            push: res.push,
            join: res.join,
            user: res.account
        })
    },
    // 账户充值
    async deposit() {
        let that = this
        const res = await request({
            url: "/deposit",
            data: this.Data2,
            method: "get"
        })
        wx.showToast({
            title: '充值成功',
            icon: 'success',
            duration: 1500,
        })
        setTimeout(function () {
            that.getUserInfor()
        }, 1500)

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {},

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        let that = this
        this.setData({
            status: getApp().globalData.status
        })
        wx.getStorage({
            key: 'token',
            success(res) {
                that.Data2.token = res.data
                that.getUserInfor()
            },
        })
        wx.getStorage({
            key: 'userInfor',
            success(res) {
                that.setData({
                    userImg: res.data.userImg,
                    name: res.data.userName,
                    infor: true
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})